﻿using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Xml;

namespace T.FAS.Runtime.DataAccess.SQLDom
{
    /// <summary>
    /// 排序子项类
    /// </summary>
    /// <remarks>SQL语句中的排序子项</remarks>
    public class OrderByStatement : SqlElement
    {
        #region 常量


        #endregion

        #region 字段


        #endregion

        #region 构造函数

        /// <summary>
        /// 构造函数
        /// </summary>
        public OrderByStatement()
            : base()
        {
            base.CreateChildCollection();
        }

        #endregion

        #region 属性

        #endregion

        #region 方法

        #endregion

        /// <summary>
        /// 克隆
        /// </summary>
        /// <returns>左连接子项类</returns>
        public override object Clone()
        {
            var newItem = base.Clone() as OrderByStatement;
            
            foreach (SqlElement item in this.ChildCollection)
            {
                newItem.ChildCollection.Add(item.Clone() as SqlElement);
            }

            return newItem;
        }

        /// <summary>
        /// 转换成SQL
        /// </summary>
        /// <returns>SQL</returns>
        public override string ToSQL()
        {
            StringBuilder result = new StringBuilder();

            if (ChildCollection == null || ChildCollection.Count <=0)
                return result.ToString();
            result.Append(" Order by ");
            for (int i = 0; i < ChildCollection.Count; i++)
            {
                var element = ChildCollection[i];
                if (i == ChildCollection.Count - 1)
                    result.Append(element.ToSQL() );
                else
                    result.Append(element.ToSQL() + ",");
            }

            return result.ToString();
        }

       
    }

    
}
